package org.projectwithoutname.ui.wizard.pages;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.projectwithoutname.ui.wizard.pages.composite.EpisodesComposite;

public class EpisodesPage extends WizardPage {
	private EpisodesComposite container;
	private String[] seasons;
	private String seasonName;
	public EpisodesPage(String pageName, String[] seasons) {
		super(pageName);
		setTitle(pageName);
		setDescription("Insert the general information about this episode");
		setPageComplete(false);
		this.seasons = seasons;
	}

	@Override
	public void createControl(Composite parent) 
	{
		
		container = new EpisodesComposite(parent,SWT.None, seasons);
		
		container.getTextName().addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e){
			}
			@Override
			public void keyReleased(KeyEvent e){
				container.selectImage().setEnabled(container.getTextName().getText().equals("") ? false : true);
				validatePage();
			}
		});
		
		container.getComboSeasons().addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				seasonName = container.getComboSeasons().getItem(container.getComboSeasons().getSelectionIndex());
				validatePage();
			}
		});
		
		container.getDescription().addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				validatePage();
			}
		});
		
		setControl(container);

	}
	
	private  void validatePage(){
		setPageComplete(true);
		String name = container.getTextName().getText();
		String description = container.getDescription().getText();
		if(name.isEmpty() || name.equals("")){
			setPageComplete(false);
			setErrorMessage("The episode's name cannot be empty");
			return;
		}
		else if((name.charAt(0) >= 'a') || (name.charAt(0) >= 'z')){
			setPageComplete(false);
			setErrorMessage("The episode's name cannot starts with lower case");
		}
		else if((name.charAt(0) == ' ') || (name.charAt(name.length()-1) == ' ')){
			setPageComplete(false);
			setErrorMessage("The episode's name cannot starts/ends with space");
		}
		else if (description.equals("")){
			setPageComplete(false);
			setErrorMessage("Any description must to be set");
		}
		else if (seasonName == null){
			setPageComplete(false);
			setErrorMessage("You have to choose a season first");
		}
		else{
			setErrorMessage(null);
			return;
		}
			
	}
	
	public String getName(){
		return container.getTextName().getText();
	}
	
	public String getDescription(){
		return container.getDescription().getText();
	}
	
	public String getChooseSeason(){
		return seasonName;
	}
	
//	public Image getImageSet(){
//		return container.getImageIco();
//	}
	
	public String path(){
		return container.episodePath();
	}

}
